﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ClickGame.General;
using Common.Entities;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using TomShane.Neoforce.Controls;

namespace ClickGame.GUI.Controls
{
    public class FightPanel:Panel
    {
        private Texture2D _background;
        private Texture2D _walkingTexture;
        private Texture2D _fightingTexture;
        private Texture2D _dyingTexture;
        private Texture2D _standingTexture;
        private int _id = 0;
        List<FightEntityControl> _soldiers = new List<FightEntityControl>();
        private int _offsetX = 0;
        public FightPanel(Manager manager) : base(manager)
        {
            KeyPress += FightPanel_KeyPress;
        }


        protected override void OnKeyPress(KeyEventArgs e)
        {
            base.OnKeyPress(e);
            if (e.Key == Keys.Left)
            {
                _offsetX += 5;
                if (_offsetX > 0)
                    _offsetX = 0;
                Invalidate();
            }
            else if (e.Key == Keys.Right)
            {
                _offsetX -= 5;
                if (_offsetX < -1025)
                    _offsetX = -1025;
                Invalidate();
            }
        }

        public Texture2D Background
        {
            get { return _background; }
            set { _background = value; }
        }

        public Texture2D WalkingTexture
        {
            get { return _walkingTexture; }
            set { _walkingTexture = value; }
        }

        public Texture2D FightingTexture
        {
            get { return _fightingTexture; }
            set { _fightingTexture = value; }
        }

        public Texture2D DyingTexture
        {
            get { return _dyingTexture; }
            set { _dyingTexture = value; }
        }

        public Texture2D StandingTexture
        {
            get { return _standingTexture; }
            set { _standingTexture = value; }
        }

        public override void Init()
        {
            base.Init();
            //CanFocus = true;
            //Focused = true;
            //Passive = false;
            ImageBox bg = new ImageBox(Manager);
            
            bg.Init();
            //bg.Detached = true;
            bg.Left = -1000;
            bg.Top = 0;
            
            bg.Image = Background;
            bg.Height = Background.Height;
            bg.Width = Background.Width;
            bg.SizeMode = SizeMode.Normal;
            bg.CanFocus = true;
            bg.KeyPress += bg_KeyPress;
            bg.Focused = true;
            bg.MouseMove += bg_MouseMove;
            Add(bg);
            Button addSoldierBtn = new Button(Manager);
            addSoldierBtn.Init();
            addSoldierBtn.Left = 10;
            addSoldierBtn.Top = 10;
            addSoldierBtn.Height = 40;
            addSoldierBtn.Width = 120;
            addSoldierBtn.Text = "добавить перца";
            addSoldierBtn.Click += addSoldierBtn_Click;
            Add(addSoldierBtn);

            Button addSoldierBtn1 = new Button(Manager);
            addSoldierBtn1.Init();
            addSoldierBtn1.Left = 130;
            addSoldierBtn1.Top = 10;
            addSoldierBtn1.Height = 40;
            addSoldierBtn1.Width = 120;
            addSoldierBtn1.Text = "добавить вражину";
            addSoldierBtn1.Click += addSoldierBtn1_Click;
            Add(addSoldierBtn1);
        }

        void bg_MouseMove(object sender, MouseEventArgs e)
        {
            //throw new NotImplementedException();
            if (e.Button == MouseButton.Left)
            {
                if (e.State.LeftButton == ButtonState.Pressed)
                {
                    _offsetX += e.Difference.X;
                    if (_offsetX < -1025)
                        _offsetX = -1025;
                    if (_offsetX > 0)
                        _offsetX = 0;
                }
            }
        }

        void bg_KeyPress(object sender, KeyEventArgs e)
        {
            //throw new NotImplementedException();
            if (e.Key == Keys.Left)
            {
                _offsetX += 5;
                if (_offsetX > 0)
                    _offsetX = 0;
                Invalidate();
            }
            else if (e.Key == Keys.Right)
            {
                _offsetX -= 5;
                if (_offsetX < -1025)
                    _offsetX = -1025;
                Invalidate();
            }
        }

        void addSoldierBtn1_Click(object sender, TomShane.Neoforce.Controls.EventArgs e)
        {
            //throw new NotImplementedException();
            FightEntityControl fec = new FightEntityControl(Manager, 0, 10, new Point(500, 500), new Rectangle(0, 0, 50, 100), _id++, _offsetX);
            fec.WalkTexture = WalkingTexture;
            fec.DieTexture = DyingTexture;
            fec.FightTexture = FightingTexture;
            fec.StandTexture = StandingTexture;
            fec.Init();
            fec.FightEntity.PlayerOwned = false;
            Add(fec);
            _soldiers.Add(fec);
        }

        void addSoldierBtn_Click(object sender, TomShane.Neoforce.Controls.EventArgs e)
        {
            //throw new NotImplementedException();
            FightEntityControl fec = new FightEntityControl(Manager,0,10,new Point(0,500), new Rectangle(0,0,50,100), _id++,_offsetX);
            fec.WalkTexture = WalkingTexture;
            fec.DieTexture = DyingTexture;
            fec.FightTexture = FightingTexture;
            fec.StandTexture = StandingTexture;
            fec.Init();
            fec.FightEntity.PlayerOwned = true;
            Add(fec);
            _soldiers.Add(fec);
        }

        private void FightPanel_KeyPress(object sender, KeyEventArgs e)
        {
            
        }

        protected override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            foreach (FightEntityControl soldier in _soldiers)
            {
                //if (soldier.FightEntity.PlayerOwned)
                //    soldier.Left += 5;
                //else
                //    soldier.Left -= 5;
                bool canMove = true;
                foreach (FightEntityControl soldierOther in _soldiers)
                {
                    if (soldier != soldierOther)
                    {
                        if (soldier.IntersectsWith(soldierOther))
                        {
                            if (soldier.FightEntity.PlayerOwned != soldierOther.FightEntity.PlayerOwned)
                            {
                                if (soldierOther.Health > 0 && soldier.Health > 0)
                                {
                                    canMove = false;
                                    soldier.ChangeState(FighterState.Fighting);
                                    //soldierOther.ChangeState(FighterState.Fighting);
                                    if (soldier.Animation.CurrentFrame == 5 && soldier.Animation.TimePassed - 0.01f <= 0)
                                    {
                                        soldierOther.Health -= soldier.FightEntity.Damage;
                                    }
                                }
                            }
                            else
                            {
                                if (soldierOther.Health > 0)
                                {
                                    if (soldier.Left <= soldierOther.Left && soldier.FightEntity.PlayerOwned)
                                    {
                                        if (soldier.Left + soldier.Width >= soldierOther.Left)
                                            if (soldier.Id > soldierOther.Id)
                                                canMove = false;
                                    }
                                    else if (soldier.Left <= soldierOther.Left + soldierOther.Width &&
                                             !soldier.FightEntity.PlayerOwned)
                                    {

                                        if (soldier.Id > soldierOther.Id)
                                            canMove = false;
                                        //canMove = false;
                                    }
                                }
                            }
                        }
                    }
                    
                }
                if (soldier.Health <= 0)
                {
                    canMove = false;
                }
                if (canMove)
                {
                    soldier.ChangeState(FighterState.Walking);
                    if (soldier.FightEntity.PlayerOwned)
                        soldier.Left += 1;
                    else
                        soldier.Left -= 1;
                    
                }
                else
                {
                    if (soldier.Health <= 0)
                    {
                        soldier.ChangeState(FighterState.Dying);
                    }
                    else
                    {
                        if(soldier.FightEntity.State != FighterState.Fighting)
                            soldier.ChangeState(FighterState.Standing);
                    }
                }
                
                soldier.FightEntity.BoundingRect.X = soldier.Left + _offsetX;
            }
            foreach (Control c in ClientArea.Controls)
            //foreach (Control c in Controls)
            {
                if (c is FightEntityControl || c is ImageBox)
                {
                    c.LeftModifier = _offsetX;
                    c.Invalidate();
                }
            }
        }

        protected override void DrawControl(Renderer renderer, Rectangle rect, GameTime gameTime)
        {
            base.DrawControl(renderer, rect, gameTime);
            //TODO: draw background
            
        }
    }
}
